from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import List

mcp = FastMCP("Fluentd MCP Server")

"""
面向软件：fluentd (rubygem-fluentd)

所有 MCP tool 列表：
1. fluent_cap_ctl_help - 获取 fluent-cap-ctl 命令的帮助信息
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

2. fluent_cap_ctl_add - 添加指定的 capability
   参数：capability (str) - 要添加的 capability 名称，如 "dac_read_search"
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

3. fluent_cap_ctl_drop - 删除指定的 capability
   参数：capability (str) - 要删除的 capability 名称，如 "dac_read_search"
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

4. fluent_cap_ctl_clear - 清除所有 capabilities
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

5. fluent_cap_ctl_get - 获取当前有效的 capabilities
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

6. fluent_cap_ctl_file - 对指定文件应用 capabilities
   参数：file_path (str) - 要应用 capabilities 的文件路径
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
"""

@mcp.tool()
def fluent_cap_ctl_help() -> dict:
    """获取 fluent-cap-ctl 命令的帮助信息
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["fluent-cap-ctl", "--help"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def fluent_cap_ctl_add(capability: str) -> dict:
    """添加指定的 capability
    
    参数：
        capability (str): 要添加的 capability 名称，如 "dac_read_search"
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["fluent-cap-ctl", "--add", capability]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def fluent_cap_ctl_drop(capability: str) -> dict:
    """删除指定的 capability
    
    参数：
        capability (str): 要删除的 capability 名称，如 "dac_read_search"
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["fluent-cap-ctl", "--drop", capability]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def fluent_cap_ctl_clear() -> dict:
    """清除所有 capabilities
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["fluent-cap-ctl", "--clear"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def fluent_cap_ctl_get() -> dict:
    """获取当前有效的 capabilities
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["fluent-cap-ctl", "--get"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def fluent_cap_ctl_file(file_path: str) -> dict:
    """对指定文件应用 capabilities
    
    参数：
        file_path (str): 要应用 capabilities 的文件路径
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["fluent-cap-ctl", "--file", file_path]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

if __name__ == "__main__":
    mcp.run()